/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.customize.redis.constants.CommonRedisConstants;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import com.pig4cloud.pigx.user.entity.Currency;
import com.pig4cloud.pigx.user.mapper.CurrencyMapper;
import com.pig4cloud.pigx.user.service.CurrencyService;
import com.pig4cloud.pigx.user.vo.AppCurrencyVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 币种
 *
 * @author pigx code generator
 * @date 2022-11-21 15:50:12
 */
@Service
@AllArgsConstructor
public class CurrencyServiceImpl extends ServiceImpl<CurrencyMapper, Currency> implements CurrencyService {

	private final IdWorker idWorker;

	private final RedisTemplate redis;

	@Override
	public R<AppCurrencyVO> getAppCurrency(String currency, String chain) {
		try {
			Currency one = getCurrency(currency,chain);
			if (one == null) {
				return null;
			}
			AppCurrencyVO vo = new AppCurrencyVO();
			BeanUtils.copyProperties(one,vo);
			return R.ok(vo);
		} catch (DuckweedException ignored) {
			throw ignored;
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

	/**
	 * 根据名称获取币种信息
	 * @param currency 币种
	 * @return detail
	 */
	@Override
	public Currency getCurrency(String currency, String chain) {
		try {
			return  baseMapper.selectOne(new QueryWrapper<Currency>() {{
				eq("name", currency);
				eq("public_chain", chain);
				eq("state", CommonDictEnum.Status.NORMAL.getValue());
			}});
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public R<String> saveCurrency(Currency currency) {
		try {
			Long selectCount = baseMapper.selectCount(new QueryWrapper<Currency>() {{
				eq("name", currency.getName());
			}});
			if (selectCount > 0) {
				throw new DuckweedException(AbnormalEnum.MSG.UNKNOWN.getCode(),"币种名称重复");
			}
			currency.setId(idWorker.nextId());
			currency.setState(CommonDictEnum.Status.DEPRECATED.getValue());
			baseMapper.insert(currency);
			if (currency.getLastPrice() != null) {
				redis.opsForValue().set(CommonRedisConstants.LAST_PRICE + currency.getName(),currency.getLastPrice());
			}
			return R.ok("新增币种成功");
		} catch (DuckweedException e) {
			throw e;
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

	@Override
	public R<String> updateCurrency(Currency currency) {
		try {
			Long selectCount = baseMapper.selectCount(new QueryWrapper<Currency>() {{
				eq("name", currency.getName());
				ne("id",currency.getId());
			}});
			if (selectCount > 0) {
				throw new DuckweedException(AbnormalEnum.MSG.UNKNOWN.getCode(),"币种名称重复");
			}
			baseMapper.updateById(currency);
			if (currency.getLastPrice() != null) {
				redis.opsForValue().set(CommonRedisConstants.LAST_PRICE + currency.getName(),currency.getLastPrice());
			}
			return R.ok("修改币种成功");
		} catch (DuckweedException e) {
			throw e;
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

}
